package com.weilele.mvvm.widget

import android.content.Context
import android.util.AttributeSet
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.weilele.mvvm.utils.activity.enableDefaultLayoutAnim

/**
 * TabLayoutMediator
 */
open class BaseTabLayout : TabLayout {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    fun attachViewPager2(viewPage2: ViewPager2, getTitle: ((tab: Tab, position: Int) -> String?)? = null) {
        TabLayoutMediator(this, viewPage2, true) { tab: Tab, position: Int ->
            tab.text = getTitle?.invoke(tab, position)
        }.attach()
    }

    init {
        enableDefaultLayoutAnim()
    }
}